package com.lefevre.cms.web.action.user;

import com.lefevre.cms.bean.user.PointLog;
import com.lefevre.cms.utils.UUIDUtil;
import com.lefevre.cms.utils.Verification;
import org.springframework.stereotype.Component;

/**
 * 积分管理
 */
@Component("pointManage")
public class PointManage {

    /**
     * 取得积分日志Id的用户Id(后N位)
     *
     * @param pointLogId 积分日志Id
     * @return
     */
    public int getPointLogUserId(String pointLogId) {
        String after_userId = pointLogId.substring(pointLogId.length() - 4, pointLogId.length());
        return Integer.parseInt(after_userId);
    }

    /**
     * 生成积分Id
     * 积分Id由32位uuid+1位用户Id后4位组成
     *
     * @param userId 用户Id
     * @return
     */
    public String createPointLogId(Long userId) {
        //选取得后N位用户Id
        String afterUserId = String.format("%04d", userId % 10000);
        String id = UUIDUtil.getUUID32() + afterUserId;
        return id;
    }

    /**
     * 校验积分Id
     * 积分Id要先判断是否36位并且最后4位是数字
     *
     * @param pointLogId 积分Id
     * @return
     */
    public boolean verificationPointLogId(String pointLogId) {
        if (pointLogId != null && !"".equals(pointLogId.trim())) {
            if (pointLogId.length() == 36) {
                String after_userId = pointLogId.substring(pointLogId.length() - 4, pointLogId.length());
                return Verification.isPositiveIntegerZero(after_userId);
            }
        }
        return false;
    }

    /**
     * 生成积分日志对象
     *
     * @return
     */
    public Object createPointLogObject(PointLog pointLog) {
        return pointLog;
    }

}
